<?php

class ControllerNewsDetail extends Controller {

    public function index() {
        //--------global------------------------

        $this->load->model('tool/custom');
        $this->load->model('tool/seo_url');
        $this->language->load('news/category_news');
        $this->load->model('catalog/news');
        $this->load->model('tool/image');
        //get id from seo_name

        $news_id = $this->model_tool_custom->SeonameToId('news_description', $this->request->get['seo_name'], 'news_id');


        //load data news from news_id
        $news = $this->load->model_catalog_news->getnews($news_id);

        //----------breakcrum-----------------------
        $this->data['breadcrumbs'][] = array(
            'href' => HTTP_SERVER,
            'text' => $this->language->get('text_home'),
            'separator' => FALSE
        );
        $this->data['breadcrumbs'][] = array(
            'href' => HTTP_SERVER . 'news/',
            'text' => $this->language->get('text_news'),
            'separator' => $this->language->get('text_separator')
        );
        $this->data['breadcrumbs'][] = array(
            'href' => HTTP_SERVER . 'news/' . $news['news_category'] . '.html',
            'text' => $news['category_name'],
            'separator' => $this->language->get('text_separator')
        );
        $this->data['breadcrumbs'][] = array(
            'href' => HTTP_SERVER . 'news/' . $news['seo_name'] . '.html',
            'text' => $news['title'],
            'separator' => $this->language->get('text_separator')
        );


        $this->document->setTitle($news['title']);

        $this->data['heading_title'] = $news['title'];

        $first_img = '';
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', html_entity_decode($news['description'], ENT_QUOTES, 'UTF-8'), $matches);
        if ($matches [1]) {
            $first_img = $matches [1] [0];
        } else {
            $first_img = "image/no_image.jpg";
        }
        $this->data['news'] = array(
            'item_id' => $news['news_id'],
            'title' => $news['title'],
            'date_added' => date('h:iA d/m/Y', strtotime($news['date_added'])),
            'description' => html_entity_decode($news['description']),
            'date_added' => date('d/m/Y', strtotime($news['date_added'])),
            'image' => $first_img,
            'href' => $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('news/news&news_id=' . $news['news_id']))
        );
        // tin lien quan
        $this->load->model('catalog/news');
        $this->load->model('catalog/category_news');
        $this->data['tinmois'] = array();
        $this->data['tinkhacs'] = array();
        if ($news['category_news_id']) {
            $category_news_id = $news['category_news_id'];
        } else {
            $category_news_id = 0;
        }

        $category_news_info = $this->model_catalog_category_news->getCategory($category_news_id);

        if ($category_news_info) {
            $this->data['category_news_name'] = $category_news_info['name'];
            $this->data['category_news_href'] = $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('news/category_news&path_news=' . $category_news_id));
            $result0s = $this->model_catalog_news->getnewssbyCatalogs($category_news_id);
            $i = 0;
            $j = 0;
            foreach ($result0s as $result) {
                if ($news_id == $result['news_id']) {
                    $j = $i;
                }
                $i++;
            }
            if ($j <= 5) {
                $j = 5;
            }
            $result1s = $this->model_catalog_news->getnewsslienquanbyCatalog($category_news_id, 0, 5);
            $result2s = $this->model_catalog_news->getnewsslienquanbyCatalog($category_news_id, $j, 10);

            foreach ($result1s as $result) {
                $this->data['tinmois'][] = array(
                    'title' => $result['title'],
                    'date_added' => date('d/m/Y', strtotime($result['date_added'])),
                    'href' => HTTP_SERVER . 'news/' . $result['news_category'] . '/' . $result['seo_name'] . '.html',
                );
            }
            foreach ($result2s as $result) {
                $this->data['tinkhacs'][] = array(
                    'title' => $result['title'],
                    'date_added' => date('d/m/Y', strtotime($result['date_added'])),
                    'href' => HTTP_SERVER . 'news/' . $result['news_category'] . '/' . $result['seo_name'] . '.html',
                );
            }
        }
        // het tin lien quan
        //--------------load template-------------------------------------------------------
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/detail.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/news/detail.tpl';
        } else {
            $this->template = 'default/template/news/detail.tpl';
        }

//        $this->children = array(
//            'common/header',
//            'common/footer',
//            'common/column_left',
//            'common/column_right'
//        );
        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );

        $this->response->setOutput($this->render());
    }

}

?>